﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using System.Collections.Generic;
using AAEngine.Engine;
using AAEngine.Rendering2D.Renderers;

namespace AAEngine.Rendering2D
{
    public class SceneLayer : ISceneLayer
    {
        private List<RendererBase> _renderableObjects = new List<RendererBase>();
        private bool _needSort = false;

        public SceneLayer()
        {
            DrawOrderComparer = new DefaultDrawOrderComparer();
        }

        public IComparer<RendererBase> DrawOrderComparer { get; set; }

        public void Add(RendererBase renderableObject)
        {
            if (_renderableObjects.Contains(renderableObject))
            {
                throw new AAException("Already added!");
            }

            _renderableObjects.Add(renderableObject);

            MarkDirty();
        }

        public void Remove(RendererBase renderableObject)
        {
            _renderableObjects.Remove(renderableObject);
        }

        public void RenderLayer(Box worldExtents, RenderQueue renderQueue)
        {
            IList<RendererBase> results = new List<RendererBase>();

            if (QueryRectangle(worldExtents, ref results))
            {
                foreach (RendererBase ro in results)
                {
                    ro.Render(worldExtents, renderQueue);
                }
            }
        }

        bool QueryRectangle(Box box, ref IList<RendererBase> results)
        {
            bool foundAny = false;

            foreach (var ro in _renderableObjects)
            {
                if (ro != null && !box.Intersects(ro.WorldExtents))
                    continue;

                results.Add(ro);
                foundAny = true;
            }

            return foundAny;
        }

        //public bool QueryCircle(Vector2 center, int radius, ref IList<IRenderable> results)
        //{
        //    bool foundAny = false;

        //    Box scratchRect = new Box();

        //    foreach (var o in _renderableObjects)
        //    {
        //        var ro = o as IRenderable2D;

        //        if (ro != null)
        //        {
        //            scratchRect.X = ro.WorldExtents.X;
        //            scratchRect.Y = ro.WorldExtents.Y;
        //            scratchRect.Width = ro.WorldExtents.Width;
        //            scratchRect.Height = ro.WorldExtents.Height;

        //            scratchRect.Inflate(radius, radius);
        //            if (scratchRect.Contains(center) == ContainmentType.Disjoint)
        //                continue;
        //        }

        //        results.Add(o);
        //        foundAny = true;
        //    }

        //    return foundAny;
        //}

        //public bool QueryPoint(Vector2 point, ref IList<IRenderable> results)
        //{
        //    bool foundAny = false;

        //    foreach (var o in _renderableObjects)
        //    {
        //        var ro = o as IRenderable2D;

        //        if (ro != null && ro.WorldExtents.Contains(point) == ContainmentType.Disjoint)
        //            continue;

        //        results.Add(o);
        //        foundAny = true;
        //    }

        //    return foundAny;
        //}

        public void MarkDirty()
        {
            _needSort = true;
        }

        private void UpdatedOrder()
        {
            _renderableObjects.Sort(DrawOrderComparer);
        }

        public class DefaultDrawOrderComparer : IComparer<RendererBase>
        {
            public int Compare(RendererBase a, RendererBase b)
            {
                return a.ZIndex - b.ZIndex;
            }
        }
    }
}
